package com.example.config;

import com.alibaba.fastjson2.JSONObject;
import com.example.entity.ConnectionConfig;
import com.example.utils.MonitorUtils;
import com.example.utils.NetUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

@Slf4j
@Configuration
public class ServerConfiguration implements ApplicationRunner {


    @Resource
    NetUtils net;

    @Resource
    MonitorUtils monitor;

    @Bean
    ConnectionConfig connectionConfig(){
        log.info("正在加载服务端连接配置");
        ConnectionConfig config = this.readConfigurationFromFile();
        if (config == null){
            config = this.registerToServer();
        }
        return config;
    }

    // Spring Boot 应用程序启动完成并初始化所有 Bean 之后 执行一些自定义逻辑
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("正在向服务器跟新基本信息");
        net.updateBaseDetail(monitor.monitorBaseDetail());
    }

    //向服务端注册
    private ConnectionConfig registerToServer(){
        Scanner scanner = new Scanner(System.in);
        String token, address;
        do {
            log.info("请输入需要注册的服务端访问地址");
            address = scanner.nextLine();
            log.info("请输入密钥");
            token = scanner.nextLine();
        } while (!net.registerToServer(address, token));
        ConnectionConfig config = new ConnectionConfig(address, token);
        this.saveConfigurationToFile(config);
        return config;
    }

    //将配置文件保存在本地
    private void saveConfigurationToFile(ConnectionConfig config) {
        File dir = new File("config");
        if (!dir.exists() && dir.mkdir()){
            log.info("创建目录");
        }
        File file = new File("config/server.json");
        try (FileWriter fileWriter = new FileWriter(file)) {
            fileWriter.write(JSONObject.from(config).toJSONString());
        } catch (IOException e) {
            log.error("保存配置文件时出错", e);
        }
        log.info("服务器连接信息已保存成功");
    }

    //尝试从本地文件读取配置
    private ConnectionConfig readConfigurationFromFile(){
        File configurationFile = new File("config/server.json");
        if (configurationFile.exists()){
            try (FileInputStream stream = new FileInputStream(configurationFile)) {
                String raw = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
                return JSONObject.parseObject(raw).to(ConnectionConfig.class);
            } catch (IOException e) {
                log.error("配置文字出错", e);
            }
        }
        return null;
    }
}
